package com.bird.user.application.converter

import cn.hutool.core.util.RandomUtil
import com.bird.user.application.pl.*
import com.bird.user.domain.vo.Nickname
import com.bird.user.domain.vo.Phone
import com.bird.user.domain.UserEntity
import com.bird.user.domain.vo.UserPassword

/**
 * 注册请求转领域实体
 */
fun UserRegisterRequest.toUserEntity(): UserEntity {
    val salt = RandomUtil.randomString(6)
    return UserEntity(
        salt = salt,
        username = this.username,
        phone = this.phone?.let { Phone(it) },
        password = UserPassword.create(password, salt),
        nickname = this.nickname?.let { Nickname(it) }
    )
}


fun UserEntity.toUserInfoApiRes(): UserInfoResponse {
    return UserInfoResponse(
        this.id,
        this.username,
        this.phone?.phone,
        this.nickname?.nickname,
        this.roles
    )
}
/**
 * 获取我的用户信息
 */
fun UserEntity.toMeResponse(permissions: List<String>): MeResponse {
    return MeResponse(
        this.id!!,
        this.username,
        this.nickname?.nickname ?: "",
        roles = this.roles,
        permissions = permissions
    )
}
